package com.smile.frame.websocket.netty.service.util;

import cn.hutool.json.JSONUtil;
import com.smile.frame.websocket.netty.service.dto.ClientInfoDTO;
import com.smile.frame.websocket.netty.service.dto.WebsocketMessageDTO;
import com.smile.frame.websocket.netty.service.enums.CmdEnum;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;

/**
 * 发送消息工具类
 */
@Slf4j
public class BuilderMessageUtil {

    /**
     * 创建String类型返回数据包
     */
    public static TextWebSocketFrame buildStringMessage(ClientInfoDTO clientInfo, CmdEnum cmdEnum, String data) {
        WebsocketMessageDTO<String> message = new WebsocketMessageDTO<>();
        message.setClientInfo(clientInfo);
        message.setCmd(cmdEnum.getCode());
        message.setData(data);
        log.info("服务端响应： {}", JSONUtil.toJsonStr(message));
        return new TextWebSocketFrame(JSONUtil.toJsonStr(message));
    }

    /**
     * 错误返回数据包
     */
    public static TextWebSocketFrame buildErrorMessage(CmdEnum cmdEnum) {
        WebsocketMessageDTO<String> message = new WebsocketMessageDTO<>();
        message.setClientInfo(null);
        message.setCmd(cmdEnum.getCode());
        message.setData(cmdEnum.getInfo());
        log.info("服务端响应Error： {}", JSONUtil.toJsonStr(message));
        return new TextWebSocketFrame(JSONUtil.toJsonStr(message));
    }

}
